import { app } from 'electron'
import { dirname, sep, join } from 'path'
import { readFileSync } from 'fs'
import log from 'electron-log/main'
import dayjs from 'dayjs'
import { TransformFnParams } from 'class-transformer'
/**
 *
 * @returns 操作系统类型: 1. mac 系统， 2. windows操作系统，3. linux操作系统 4. 未知系统
 */
export function getSystem() {
  //这是mac系统
  if (process.platform == 'darwin') {
    return 1
  }
  //这是windows系统
  if (process.platform == 'win32') {
    return 2
  }
  //这是linux系统
  if (process.platform == 'linux') {
    return 3
  }
  return 4
}
/**
 *
 * @returns 获取安装路径
 */
export function getExePath() {
  return join(process.cwd(), 'resources')
}
export function getMockDownloadFilePath(type: string) {
  if (type === 'word') {
    return join(getExePath(), 'mockDownloadFile', `mock-${type}.docx`)
  } else if (type === 'execel') {
    return join(getExePath(), 'mockDownloadFile', `mock-${type}.xlsx`)
  } else {
    return join(getExePath(), 'mockDownloadFile', `mock-${type}.${type}`)
  }
}
/**
 *
 * @returns 获取配置文件路径
 */
export function getConfigPath() {
  const exeFilePath = getExePath()
  log.info(`exeFilePath: ${exeFilePath}`)
  return join(exeFilePath, sep, 'config.json')
}
/**
 * 获取快速插入代码的文件路径
 * @param fileName quickInsertMockCode目录下的文件名(不包含扩展名)
 * @returns 快速插入代码的文件路径
 */
export function getQuickInsertMockCodeFilePath(fileName: string) {
  const exeFilePath = getExePath()
  return join(exeFilePath, sep, 'quickInsertMockCode', sep, `${fileName}.js`)
}
/**
 * 获取resources/docs目录下的文档文件的文件路径
 * @param fileName 文档文件名(包含扩展名) 如: mock-js.html
 * @returns 文件路径
 */
export function getDocsFilePath(fileName: string) {
  const exeFilePath = getExePath()
  return join(exeFilePath, sep, 'docs', sep, `${fileName}`)
}
export interface HttpServer {
  port: number
}
export interface EasyMockConfig {
  httpServer: HttpServer
}
/**
 * 读取配置文件
 */
export function readConfig(): EasyMockConfig {
  const configFilePath = getConfigPath()
  const content = readFileSync(configFilePath, { encoding: 'utf-8' })
  if (content) {
    return JSON.parse(content) as EasyMockConfig
  } else {
    return {
      httpServer: {
        port: 4000,
      },
    }
  }
}

/**
 * 读取指定路径下的文件内容
 * @param filePath 文件路径
 * @returns
 */
export function readFileContent(filePath: string) {
  const content = readFileSync(filePath, { encoding: 'utf-8' })
  if (content) {
    return content
  } else {
    return ''
  }
}

export function dateFormat(date: TransformFnParams, template = 'YYYY-MM-DD HH:mm:ss') {
  if (date && date.value instanceof Date) {
    return dayjs(date.value).format(template)
  } else {
    return date
  }
}
